<template>
  <a-spin :spinning="iframeLoading" wrapper-class-name="w-full h-full" class="w-full h-full">
    <div class="flex flex-col w-full h-full overflow-hidden">
      <div class="flex justify-end my-2">
        <a-button v-if="props.isShowBack" class="mr-2" type="primary" @click="onBack">返回</a-button>
        <a-button type="primary" class="mr-4" ghost @click="onPrint">打印</a-button>
      </div>
      <div class="flex-1">
        <iframe v-if="iframeUrl" name="iframe" :src="iframeUrl" width="100%" height="100%" frameborder="0"></iframe>
      </div>
    </div>
  </a-spin>
</template>
<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { message } from 'ant-design-vue'

const props = defineProps({
  inplsh: {
    type: String,
    default: ''
  },
  isShowBack: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['back'])
const iframeLoading = ref(true)
const iframeUrl = ref('')
const postIframe = async () => {
  iframeLoading.value = true
  const { code, msg, data } = await post('/inp/Print/printBloodGlucoseTestSlips', { inplsh: props.inplsh })
  if (code !== 0) return message.error(msg)
  const res = await axios.post('/api/v1/iframe/ReportPDF.aspx', data, { responseType: 'blob' })
  iframeLoading.value = false

  const href = URL.createObjectURL(res.data)
  const element = document.createElement('a')
  element.download = '血糖监测' + '.pdf'
  element.href = href
  iframeUrl.value = href + '#scrollbars=0&navpanes=0&statusbar=0&toolbar=0'
}
const isArmy = getSystemConfig('NAVYHIS_MODE') === '1'

const onPrint = async () => {
  const { code, msg, data } = await post('/inp/Print/printBloodGlucoseTestSlips', { inplsh: props.inplsh })
  if (code !== 0) return message.error(msg)
  if (isArmy) {
    const res = await axios.post('/api/v1/iframe/ReportPDF.aspx', data, { responseType: 'blob' })
    const href = URL.createObjectURL(res.data)
    const iframe = document.createElement('iframe')
    iframe.style.display = 'none'
    iframe.src = href
    document.body.appendChild(iframe)
    iframe.contentWindow.focus()
    iframe.contentWindow.print()
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}
const onBack = () => {
  emit('back')
}
onMounted(() => {
  props.inplsh && postIframe()
})
</script>
<style scoped>
:deep(.ant-spin-container) {
  width: 100%;
  height: 100%;
}
</style>
